//
//  UIView+EQ.swift
//  EQDudao
//
//  Created by 范伟健 on 2018/5/29.
//  Copyright © 2018年 EQingLan.,Ltd. All rights reserved.
//

import UIKit
import SnapKit

struct EQAddLineType : OptionSet {
    let rawValue: Int
    
    static let EQAddTopLine = EQAddLineType(rawValue: 1 << 0)
    static let EQAddLeftLine = EQAddLineType(rawValue: 1 << 1)
    static let EQAddBottomLine = EQAddLineType(rawValue: 1 << 2)
    static let EQAddRightLine = EQAddLineType(rawValue: 1 << 3)
    static let EQAddAllLine = EQAddLineType(rawValue: 2)
}

extension UIView : EQCompatible{}

extension EQinglan where Base : UIView  {
    
    
    /// 添加边线
    ///
    /// - Parameters:
    ///   - type: 添加边线的位置
    ///   - width: 边线宽度
    ///   - color: 边线颜色
    func addLineAtAutoLayout(_ type : EQAddLineType , _ width : CGFloat , _ color : UIColor)  {
        var lineType = type
        if lineType.contains(.EQAddAllLine) {
            lineType = [.EQAddTopLine, .EQAddLeftLine, .EQAddBottomLine , .EQAddBottomLine]
        }
        
        func creatLineView() -> UIView {
            let line = UIView()
            line.backgroundColor = color
            base.addSubview(line)
            return line
        }
        
        if lineType.contains(.EQAddTopLine) {
            let line = creatLineView()
            line.tag = 199001
            line.snp.makeConstraints { (make) in
                make.left.top.right.equalToSuperview()
                make.height.equalTo(width)
            }
        }
        
        if lineType.contains(.EQAddLeftLine) {
            let line = creatLineView()
            line.tag = 199002
            line.snp.makeConstraints { (make) in
                make.top.bottom.left.equalToSuperview()
                make.width.equalTo(width)
            }
        }
        
        if lineType.contains(.EQAddBottomLine) {
            let line = creatLineView()
            line.tag = 199003
            line.snp.makeConstraints { (make) in
                make.left.right.bottom.equalToSuperview()
                make.height.equalTo(width)
            }
        }
        if lineType.contains(.EQAddRightLine) {
            let line = creatLineView()
            line.tag = 199004
            line.snp.makeConstraints { (make) in
                make.top.bottom.right.equalToSuperview()
                make.width.equalTo(width)
            }
        }
    }
    
    
    /// 删除所有边线
    func removeAllLine(){
        for subView in base.subviews {
            if [199001,199002,199003,199004].contains(subView.tag) {
                subView.removeFromSuperview()
            }
        }
    }
    
    /// 添加竖直方向渐变背景
    ///
    /// - Parameters:
    ///   - colors: 渐变颜色数组
    ///   - locations: 渐变位置数组
    func addVerticalGradient(_ colors:[CGColor] , _ locations : [NSNumber]) {
        base.setNeedsLayout()
        base.layoutIfNeeded()
        let gradientLayer = CAGradientLayer()
        gradientLayer.colors = colors
        gradientLayer.locations = locations
        gradientLayer.frame = base.bounds
        gradientLayer.startPoint = CGPoint.init(x: 0, y: 0)
        gradientLayer.endPoint = CGPoint.init(x: 0, y: 1)
        base.layer.insertSublayer(gradientLayer, at: 0)
        
    }
    
    
    /// 添加水平方向渐变背景
    ///
    /// - Parameters:
    ///   - colors: 渐变颜色数组
    ///   - locations: 渐变位置数组
    func addHorizontalGradient(_ colors:[CGColor] , _ locations : [NSNumber]) {
        base.setNeedsLayout()
        base.layoutIfNeeded()
        let gradientLayer = CAGradientLayer()
        gradientLayer.colors = colors
        gradientLayer.locations = locations
        gradientLayer.frame = base.bounds
        gradientLayer.startPoint = CGPoint.init(x: 0, y: 0)
        gradientLayer.endPoint = CGPoint.init(x: 1, y: 0)
        base.layer.insertSublayer(gradientLayer, at: 0)
    }
    
    
    
    
    
}

